//Set your directory
cd xx

use experimental_data, clear

gen id=_n
gen x1=1
gen x2=1

reshape long x, i(id) j(profile)
drop x*

*Respondent demographics
gen republican_r=0
 replace republican_r=1 if party=="Republican"
gen democrat_r=0
 replace democrat_r=1 if party=="Democrat"
gen independent_r=0
 replace independent_r=1 if party=="Independent"
 replace independent_r=1 if party=="Something else"
 replace independent_r=1 if party==""
gen party_r=""
 replace party_r="Republican" if republican_r==1
 replace party_r="Democrat" if democrat_r==1
 replace party_r="Independent" if independent_r==1
  
gen female_r=0
 replace female_r=1 if gender=="Female"
gen white_r=0
 replace white_r=1 if race=="White"
gen race2=cond(race=="White","white",cond(strpos(race,"Black")>0,"black",cond(strpos(race,"Hispanic")>0,"hispanic","other")))
gen black_r=0
 replace black_r=1 if strpos(race, "Black")>0
gen hispanic_r=0
 replace hispanic_r=1 if strpos(race, "Hispanic")>0 & black_r==0
gen college_r=0
 replace college_r=1 if educ=="4 year college degree" | educ=="Professional degree" | educ=="Doctorate"
 
//code political knowledge question
gen pk=0
replace pk=pk+1 if strpos(pk1,"China")>0 & pk1!=""
replace pk=pk+1 if strpos(pk1,"France")>0 & pk1!=""
replace pk=pk+1 if strpos(pk1,"Brazil")==0 & pk1!=""
replace pk=pk+1 if strpos(pk1,"Japan")>0 & pk1!=""
replace pk=pk+1 if strpos(pk2,"Pompeo")>0 & pk2!=""
replace pk=pk+1 if strpos(pk2,"Tillerson")>0 & pk2!=""
replace pk=pk+1 if strpos(pk2,"Johnson")==0 & pk2!=""
replace pk=pk+1 if strpos(pk2,"Powell")==0 & pk2!=""
replace pk=pk+1 if strpos(pk3,"Afghanistan")>0 & pk3!=""
replace pk=pk+1 if strpos(pk3,"Germany")>0 & pk3!=""
replace pk=pk+1 if strpos(pk3,"Japan")>0 & pk3!=""
replace pk=pk+1 if strpos(pk3,"Libya")==0 & pk3!=""

cumul pk, gen(pk_pct)
 
*DVs
foreach att in "strong"{
foreach cand in "allen" "miller"{
forvalues var=1(1)2{
gen `cand'`var'`att'=.
 replace `cand'`var'`att'=1 if `cand'_`att'`var'=="Very unlikely" | `cand'_`att'`var'=="Very Unlikely"
 replace `cand'`var'`att'=2 if `cand'_`att'`var'=="Unlikely"
 replace `cand'`var'`att'=3 if `cand'_`att'`var'=="Likely"
 replace `cand'`var'`att'=4 if `cand'_`att'`var'=="Very likely" | `cand'_`att'`var'=="Very Likely"
 replace `cand'`var'`att'=. if `cand'_`att'`var'=="Don't Know / No Opinion"
}
}
}
drop strong1 strong2 
foreach var in "strong"{
gen `var'=cond(profile==1,(allen1`var'+5-allen2`var')/2,cond(profile==2,(miller1`var'+5-miller2`var')/2,.))
gen `var'1=cond(profile==1,allen1`var',cond(profile==2,miller1`var',.))
gen `var'2=cond(profile==1,5-allen2`var',cond(profile==2,5-miller2`var',.))
//drop jones_`var'* davis_`var'*
}

rename allen_alignemnt allen_alignment
gen alignment=.
 replace alignment=1 if profile==1 & allen_alignment=="1: Strongly disagree"
 replace alignment=2 if profile==1 & allen_alignment=="2"
 replace alignment=3 if profile==1 & allen_alignment=="3"
 replace alignment=4 if profile==1 & allen_alignment=="4: Neither agree nor disagree"
 replace alignment=5 if profile==1 & allen_alignment=="5"
 replace alignment=6 if profile==1 & allen_alignment=="6"
 replace alignment=7 if profile==1 & allen_alignment=="7: Strongly agree"
 replace alignment=1 if profile==2 & miller_alignment=="1: Strongly disagree"
 replace alignment=2 if profile==2 & miller_alignment=="2"
 replace alignment=3 if profile==2 & miller_alignment=="3"
 replace alignment=4 if profile==2 & miller_alignment=="4: Neither agree nor disagree"
 replace alignment=5 if profile==2 & miller_alignment=="5"
 replace alignment=6 if profile==2 & miller_alignment=="6"
 replace alignment=7 if profile==2 & miller_alignment=="7: Strongly agree"

gen hawkishness=cond(profile==1,allen_hawk,cond(profile==2,miller_hawk,.))

*Hawk types
gen candidate=""
replace candidate="allen" if profile==1
replace candidate="miller" if profile==2

forvalues val=1(1)31{
gen hawk_`val'=.
}
scalar i=1
foreach cand in "allen" "miller"{
forvalues val=1(1)5{
replace hawk_1=1 if strpos(`cand'_p`val',"opposes withdrawing U.S. troops from Afghanistan")>0 & profile==scalar(i)
replace hawk_1=0 if strpos(`cand'_p`val',"supports withdrawing U.S. troops from Afghanistan")>0 & profile==scalar(i)
replace hawk_2=1 if strpos(`cand'_p`val',"opposes withdrawing U.S. troops currently fighting ISIS in Syria and Iraq")>0 & profile==scalar(i)
replace hawk_2=0 if strpos(`cand'_p`val',"supports withdrawing U.S. troops currently fighting ISIS in Syria and Iraq")>0 & profile==scalar(i)
replace hawk_3=1 if strpos(`cand'_p`val',"thinks the United States should maintain its ground troops in South Korea")>0 & profile==scalar(i)
replace hawk_3=0 if strpos(`cand'_p`val',"thinks the United States should reduce its ground troops in South Korea")>0 & profile==scalar(i)
replace hawk_4=1 if strpos(`cand'_p`val',"would use U.S. troops to defend Taiwan from a Chinese attack")>0 & profile==scalar(i)
replace hawk_4=0 if strpos(`cand'_p`val',"would not use U.S. troops to defend Taiwan from a Chinese attack")>0 & profile==scalar(i)
replace hawk_5=1 if strpos(`cand'_p`val',"would use U.S. troops to stop Russia from invading NATO allies such as Latvia, Lithuania, and Estonia")>0 & profile==scalar(i)
replace hawk_5=0 if strpos(`cand'_p`val',"would not use U.S. troops to stop Russia from invading NATO allies such as Latvia, Lithuania, and Estonia")>0 & profile==scalar(i)
replace hawk_6=1 if strpos(`cand'_p`val',"would use U.S. troops if Pakistan requested assistance in suppressing a radical Islamic insurgency")>0 & profile==scalar(i)
replace hawk_6=0 if strpos(`cand'_p`val',"would not use U.S. troops if Pakistan requested assistance in suppressing a radical Islamic insurgency")>0 & profile==scalar(i)
replace hawk_7=1 if strpos(`cand'_p`val',"supports maintaining long-term military bases in the Middle East")>0 & profile==scalar(i)
replace hawk_7=0 if strpos(`cand'_p`val',"opposes maintaining long-term military bases in the Middle East")>0 & profile==scalar(i)
replace hawk_8=1 if strpos(`cand'_p`val',"would increase the frequency of drone strikes targeting suspected terrorists overseas")>0 & profile==scalar(i)
replace hawk_8=0 if strpos(`cand'_p`val',"would reduce the frequency of drone strikes targeting suspected terrorists overseas")>0 & profile==scalar(i)
replace hawk_9=1 if strpos(`cand'_p`val',"thinks it is generally justifiable to use military force against countries that pose a serious threat to U.S. national security, even if those countries have not attacked us first")>0 & profile==scalar(i)
replace hawk_9=0 if strpos(`cand'_p`val',"thinks it is not generally justifiable to use military force against countries that have not attacked us first, even if those countries pose a serious threat to U.S. national security")>0 & profile==scalar(i)
replace hawk_10=1 if strpos(`cand'_p`val',"supports a policy of trying to remove Venezuelan President Nicolas Maduro from power")>0 & profile==scalar(i)
replace hawk_10=0 if strpos(`cand'_p`val',"opposes a policy of trying to remove Venezuelan President Nicolas Maduro from power")>0 & profile==scalar(i)
replace hawk_11=1 if strpos(`cand'_p`val',"would raise overall levels of military spending")>0 & profile==scalar(i)
replace hawk_11=0 if strpos(`cand'_p`val',"would not raise overall levels of military spending")>0 & profile==scalar(i)
replace hawk_12=1 if strpos(`cand'_p`val',"thinks the United States should devote more resources to global counterterrorism")>0 & profile==scalar(i)
replace hawk_12=0 if strpos(`cand'_p`val',"thinks the United States should devote fewer resources to global counterterrorism")>0 & profile==scalar(i)
replace hawk_13=1 if strpos(`cand'_p`val',"would expand existing efforts to modernize America's nuclear arsenal")>0 & profile==scalar(i)
replace hawk_13=0 if strpos(`cand'_p`val',"would scale back existing efforts to modernize America's nuclear arsenal")>0 & profile==scalar(i)
replace hawk_14=1 if strpos(`cand'_p`val',"thinks it is important for America to remain the world's sole military superpower")>0 & profile==scalar(i)
replace hawk_14=0 if strpos(`cand'_p`val',"does not think it is important for America to remain the world's sole military superpower")>0 & profile==scalar(i)
replace hawk_15=1 if strpos(`cand'_p`val',"supports expanding the U.S. military presence in space")>0 & profile==scalar(i)
replace hawk_15=0 if strpos(`cand'_p`val',"opposes expanding the U.S. military presence in space")>0 & profile==scalar(i)
replace hawk_16=1 if strpos(`cand'_p`val',"thinks we should publicly criticize European allies who do not meet their obligations to support collective defense")>0 & profile==scalar(i)
replace hawk_16=0 if strpos(`cand'_p`val',"thinks we should avoid publicly criticizing European allies, even if they do not meet their obligations to support collective defense")>0 & profile==scalar(i)
replace hawk_17=1 if strpos(`cand'_p`val',"thinks the United States should actively work to limit China's rising power, even if that runs the risk of starting a military conflict")>0 & profile==scalar(i)
replace hawk_17=0 if strpos(`cand'_p`val',"does not think the United States should actively work to limit China's rising power if that runs the risk of starting a military conflict")>0 & profile==scalar(i)
replace hawk_18=1 if strpos(`cand'_p`val',"thinks threatening military action is the best way to prevent Iran from developing nuclear weapons")>0 & profile==scalar(i)
replace hawk_18=0 if strpos(`cand'_p`val',"thinks diplomacy and sanctions are the best way to prevent Iran from developing nuclear weapons")>0 & profile==scalar(i)
replace hawk_19=1 if strpos(`cand'_p`val',"thinks negotiating with North Korea would only reward that country's bad behavior")>0 & profile==scalar(i)
replace hawk_19=0 if strpos(`cand'_p`val',"thinks negotiating with North Korea would raise the chances of convincing that country to give up its nuclear weapons")>0 & profile==scalar(i)
replace hawk_20=1 if strpos(`cand'_p`val',"thinks the United States should not cooperate more closely with allies if that requires going along with policies that we would not otherwise support")>0 & profile==scalar(i)
replace hawk_20=0 if strpos(`cand'_p`val',"thinks the United States should cooperate more closely with allies, even if that requires going along with policies that we would not otherwise support")>0 & profile==scalar(i)
replace hawk_21=1 if strpos(`cand'_p`val',"thinks the United States should generally avoid working with the United Nations to solve global problems in cases where going it alone would allow us to do things faster and more effectively")>0 & profile==scalar(i)
replace hawk_21=0 if strpos(`cand'_p`val',"thinks the United States should generally work with the United Nations to solve global problems, even in cases where going it alone would allow us to do things faster and more effectively")>0 & profile==scalar(i)
replace hawk_22=1 if strpos(`cand'_p`val',"would increase economic sanctions on Russia")>0 & profile==scalar(i)
replace hawk_22=0 if strpos(`cand'_p`val',"would not increase economic sanctions on Russia")>0 & profile==scalar(i)
replace hawk_23=1 if strpos(`cand'_p`val',"opposes establishing a closer political and economic relationship with Cuba")>0 & profile==scalar(i)
replace hawk_23=0 if strpos(`cand'_p`val',"supports establishing a closer political and economic relationship with Cuba")>0 & profile==scalar(i)
replace hawk_24=1 if strpos(`cand'_p`val',"thinks the United States should expand its capabilities to conduct cyberattacks against other countries")>0 & profile==scalar(i)
replace hawk_24=0 if strpos(`cand'_p`val',"does not think the United States should expand its capabilities to conduct cyberattacks against other countries")>0 & profile==scalar(i)
replace hawk_25=1 if strpos(`cand'_p`val',"thinks there are circumstances under which it is acceptable to torture captured terrorists")>0 & profile==scalar(i)
replace hawk_25=0 if strpos(`cand'_p`val',"thinks there are no circumstances under which it is acceptable to torture captured terrorists")>0 & profile==scalar(i)
replace hawk_26=1 if strpos(`cand'_p`val',"thinks that military strength is a better way to ensure peace in comparison to diplomacy")>0 & profile==scalar(i)
replace hawk_26=0 if strpos(`cand'_p`val',"thinks that diplomacy is a better way to ensure peace in comparison to military strength")>0 & profile==scalar(i)
}
scalar i=scalar(i)+1
}

*Candidate Demographics
gen republican_c=0
 replace republican_c=1 if profile==1 & allen_party=="Republican"
 replace republican_c=1 if profile==2 & miller_party=="Republican"

gen copartisan_c=0
 replace copartisan_c=1 if republican_c==1 & republican_r==1
 replace copartisan_c=1 if republican_c==0 & democrat_r==1

gen coideology_c=0
 replace coideology_c=1 if republican_c==1 & ideology=="Conservative"
 replace coideology_c=1 if republican_c==0 & ideology=="Liberal"
 
gen female_c=0
 replace female_c=1 if profile==1 & allen_pronoun=="She"
 replace female_c=1 if profile==2 & miller_pronoun=="She"
gen govt_c=0
 forvalues years=1(1)30{
 replace govt_c=`years' if profile==1 & strpos(allen_govt,"`years' years")>0
 replace govt_c=`years' if profile==2 & strpos(miller_govt,"`years' years")>0
  }
gen govtcat_c=cond(govt_c==0,0,cond(govt_c<11,1,cond(govt_c<21,2,3)))

gen mil_c=0
 forvalues years=1(1)30{
 replace mil_c=`years' if profile==1 & strpos(allen_mil,"`years' years")>0
 replace mil_c=`years' if profile==2 & strpos(miller_mil,"`years' years")>0
 }
gen milcat_c=cond(mil_c==0,0,cond(mil_c<11,1,cond(mil_c<21,2,3)))

gen nogovt_c=cond(govtcat==0,1,0)
gen nomil_c=cond(milcat==0,1,0)

gen femalexrep=female_r*republican_c
gen blackxrep=black_r*republican_c

*WEIGHTING
gen all=1
total all
scalar n = _b[all]

gen racetot=.
scalar p1=round(scalar(n)*.12)
 replace racetot=scalar(p1) if race2=="black"
scalar p2=round(scalar(n)*.16)
 replace racetot=scalar(p2) if race2=="hispanic"
scalar p3=round(scalar(n)*.09)
 replace racetot=scalar(p3) if race2=="other"
scalar p4=round(scalar(n)*.63)
 replace racetot=scalar(p4) if race2=="white"
scalar remainder=scalar(n)-scalar(p1)-scalar(p2)-scalar(p3)-scalar(p4)
 replace racetot=racetot+scalar(remainder) if race2=="white"
  
gen gendertot=round(scalar(n)*.5)
 scalar remainder=scalar(n)-2*round(scalar(n)*.5)
replace gendertot=gendertot+scalar(remainder) if female_r==1

gen collegetot=. 
scalar p1=round(scalar(n)*.36)
 replace collegetot=scalar(p1) if college_r==1
scalar p2=round(scalar(n)*.64)
 replace collegetot=scalar(p2) if college_r==0
scalar remainder=scalar(n)-scalar(p1)-scalar(p2)
 replace collegetot=collegetot+scalar(remainder) if college_r==1

gen partytot=.
scalar p1=round(scalar(n)*.33)
 replace partytot=scalar(p1) if democrat_r==1
scalar p2=round(scalar(n)*.29)
 replace partytot=scalar(p2) if republican_r==1
scalar p3=round(scalar(n)*.38)
 replace partytot=scalar(p3) if independent_r==1
scalar remainder=scalar(n)-scalar(p1)-scalar(p2)-scalar(p3)
 replace partytot=partytot+scalar(remainder) if republican_r==1

gen agetot=.
scalar p1=round(scalar(n)*.12)
 replace agetot=scalar(p1) if age=="18-24"
scalar p2=round(scalar(n)*.16)
 replace agetot=scalar(p2) if age=="25-34"
scalar p3=round(scalar(n)*.18)
 replace agetot=scalar(p3) if age=="35-44"
scalar p4=round(scalar(n)*.19)
 replace agetot=scalar(p4) if age=="45-54"
scalar p5=round(scalar(n)*.16)
 replace agetot=scalar(p5) if age=="55-64"
scalar p6=round(scalar(n)*.19)
 replace agetot=scalar(p6) if age=="65 and over"
scalar remainder=scalar(n)-scalar(p1)-scalar(p2)-scalar(p3)-scalar(p4)-scalar(p5)-scalar(p6)
 replace partytot=partytot+scalar(remainder) if age=="45-54"

gen equalweights=1
survwgt rake equalweights, by(age race2 female_r party_r college_r) totvar(agetot racetot gendertot partytot collegetot) gen(rakewt)

*ANALYSIS
svyset id [pweight=rakewt]

//adding one additional hawkish policy increases perceived ldr strength by 0.08 points
svy: reg strong hawkishness

//Footnote 49
svy: reg strong female_c govt_c mil_c copartisan_c

*Fig 5
matrix strongstore=J(6,7,.)
svy: reg strong hawkishness
margins, at(hawkishness=(0(1)5)) vce(unconditional) post
forvalues val=1(1)6{
matrix strongstore[`val',1]=`val'-1
matrix strongstore[`val',2]=_b[`val'._at]
matrix strongstore[`val',3]=_b[`val'._at]+invttail(e(df_r),0.025)*_se[`val'._at]
matrix strongstore[`val',4]=_b[`val'._at]-invttail(e(df_r),0.025)*_se[`val'._at]
}
svy: reg strong i.hawkishness
margins, at(hawkishness=(0(1)5)) vce(unconditional) post
forvalues val=1(1)6{
matrix strongstore[`val',5]=_b[`val'._at]
matrix strongstore[`val',6]=_b[`val'._at]+invttail(e(df_r),0.025)*_se[`val'._at]
matrix strongstore[`val',7]=_b[`val'._at]-invttail(e(df_r),0.025)*_se[`val'._at]
}
svmat strongstore

twoway rcap strongstore4 strongstore3 strongstore1, recast(rarea) acolor(gs10) || lfit strongstore2 strongstore1, lcolor(black) xlabel(,labsize(medlarge)) ylabel(2.25 "2.25" 2.5 "2.5" 2.75 "2.75" 3.0 "3.0",labsize(medlarge)) title("(a)"" ", size(medlarge)) xtitle(" ""Candidate Hawkishness", size(medlarge)) ytitle("Leadership Strength"" ",size(medlarge)) xsize(3) ysize(3) plotregion(lwidth(none)) leg(off) ||scatter strongstore5 strongstore1, msymbol(circle) mcolor(gs5) || rcap strongstore7 strongstore6 strongstore1, msize(0) lcolor(gs5)
 graph export fig5a.tif, replace

svy: reg alignment hawkishness
matrix alignstore=J(6,7,.)
svy: reg alignment hawkishness 
margins, at(hawkishness=(0(1)5)) vce(unconditional) post
forvalues val=1(1)6{
matrix alignstore[`val',1]=`val'-1
matrix alignstore[`val',2]=_b[`val'._at]
matrix alignstore[`val',3]=_b[`val'._at]+invttail(e(df_r),0.025)*_se[`val'._at]
matrix alignstore[`val',4]=_b[`val'._at]-invttail(e(df_r),0.025)*_se[`val'._at]
}
svy: reg alignment i.hawkishness 
margins, at(hawkishness=(0(1)5)) vce(unconditional) post
forvalues val=1(1)6{
matrix alignstore[`val',5]=_b[`val'._at]
matrix alignstore[`val',6]=_b[`val'._at]+invttail(e(df_r),0.025)*_se[`val'._at]
matrix alignstore[`val',7]=_b[`val'._at]-invttail(e(df_r),0.025)*_se[`val'._at]
}
svmat alignstore

twoway rcap alignstore4 alignstore3 alignstore1, recast(rarea) acolor(gs10) || lfit alignstore2 alignstore1, lcolor(black) xlabel(,labsize(medlarge)) ylabel(3.5 "3.5" 4 "4.0" 4.5 "4.5" 5 "5.0" ,labsize(medlarge)) title("(b)"" ", size(medlarge)) xtitle(" ""Candidate Hawkishness", size(medlarge)) ytitle("Preference Alignment"" ",size(medlarge)) xsize(3) ysize(3) plotregion(lwidth(none)) leg(off) ||scatter alignstore5 alignstore1, msymbol(circle) mcolor(gs5) || rcap alignstore7 alignstore6 alignstore1, msize(0) lcolor(gs5)
 graph export fig5b.tif, replace
 
//relationship between hawkishness and preference alignment
svy: reg alignment hawkishness

//Afghanistan
svy: reg alignment hawk_1 // hawks on Afgh receive worse alignment scores
svy: reg strong hawk_1 // but hawks on Afgh seen as stronger leaders

*Fig 6
matrix hawker=J(26,5,.)
forvalues val=1(1)26{
matrix hawker[`val',1]=`val'
svy: reg strong hawk_`val' 
 matrix hawker[`val',2]=_b[hawk]
svy: reg alignment hawk_`val'
 matrix hawker[`val',4]=_b[hawk]
 }
svmat hawker
rename hawker1 hawker_index  
rename hawker2 hawker_strong
rename hawker4 hawker_alignment 
gen hawker_label=cond(_n==1,"A","")

scatter hawker_strong hawker_alignment, mlabel(hawker_label) mlabp(12) mlabg(1) mlabs(medsmall) ylabel(-.1 "-0.1" "0" .1 "+0.1" .2 "+0.2" .3 "+0.3", labsize(medium) angle(horiz)) xlabel(-.5 "-0.50" -.25 "-0.25" 0 "0" .25 "+0.25" .5 "+0.50", labsize(medium)) ytitle("Impact of hawkish position""on perceptions of Leadership Strength"" ", size(medium)) xtitle(" ""Impact of hawkish position on Preference Alignment", size(medium)) mcolor(black) plotregion(lwidth(none)) xline(0, lpattern(dot)) yline(0, lpattern(dot))  ysize(3) xsize(5)
 graph export fig6.tif, replace

//footnote 52
reg hawker_strong hawker_alignment

//Footnote 53
reg strong hawkishness if black_r==1
reg alignment hawkishness if black_r==1

//Robustness to other demographics
reg strong hawkishness if female_r==0
reg strong hawkishness if female_r==1
reg strong hawkishness if white_r==1
reg strong hawkishness if hispanic_r==1
reg strong hawkishness if college_r==1
reg strong hawkishness if college_r==0
reg strong hawkishness if pk_pct>.5
reg strong hawkishness if pk_pct<.5
reg strong hawkishness if democrat_r==1
reg strong hawkishness if republican_r==1
reg strong hawkishness if independent_r==1

reg alignment hawkishness if female_r==0
reg alignment hawkishness if female_r==1
reg alignment hawkishness if white_r==1
reg alignment hawkishness if hispanic_r==1
reg alignment hawkishness if college_r==1
reg alignment hawkishness if college_r==0
reg alignment hawkishness if pk_pct>.5
reg alignment hawkishness if pk_pct<.5
reg alignment hawkishness if democrat_r==1
reg alignment hawkishness if republican_r==1
reg alignment hawkishness if independent_r==1

 